package com.moxiao.yfjh.module.activity.controller.app;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.activity.controller.admin.activityPackage.vo.*;
import com.moxiao.yfjh.module.activity.convert.activityPackage.ActivityPackageConvert;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityPackage.ActivityPackageDO;
import com.moxiao.yfjh.module.activity.service.activityPackage.ActivityPackageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "app - 相亲活动套餐信息")
@RestController
@RequestMapping("/activity/package")
@Validated
public class AppActivityPackageController {

    @Resource
    private ActivityPackageService packageService;


    @PostMapping("/create")
    @Operation(summary = "创建相亲活动套餐信息")
    @PreAuthorize("@ss.hasPermission('activity:package:create')")
    public CommonResult<Boolean> createPackage(@Valid @RequestBody List<ActivityPackageCreateReqVO> createReqVO) {
        packageService.createPackage(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲活动套餐信息")
    @PreAuthorize("@ss.hasPermission('activity:package:update')")
    public CommonResult<Boolean> updatePackage(@Valid @RequestBody List<ActivityPackageUpdateReqVO> updateReqVO) {
        packageService.updatePackage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲活动套餐信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('activity:package:delete')")
    public CommonResult<Boolean> deletePackage(@RequestParam("id") Long id) {
        packageService.deletePackage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲活动套餐信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('activity:package:query')")
    public CommonResult<ActivityPackageRespVO> getPackage(@RequestParam("id") Long id) {
        ActivityPackageDO packageInfo = packageService.getPackage(id);
        ActivityPackageRespVO respVO = ActivityPackageConvert.INSTANCE.convert(packageInfo);

        return success(respVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲活动套餐信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('activity:package:query')")
    public CommonResult<List<ActivityPackageRespVO>> getPackageList(@RequestParam("ids") Collection<Long> ids) {
        List<ActivityPackageDO> list = packageService.getPackageList(ids);
        return success(ActivityPackageConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲活动套餐信息分页")
    @PreAuthorize("@ss.hasPermission('activity:package:query')")
    public CommonResult<PageResult<ActivityPackageRespVO>> getPackagePage(@Valid ActivityPackagePageReqVO pageVO) {
        PageResult<ActivityPackageDO> pageResult = packageService.getPackagePage(pageVO);
        return success(ActivityPackageConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲活动套餐信息 Excel")
    @PreAuthorize("@ss.hasPermission('activity:package:export')")
    @OperateLog(type = EXPORT)
    public void exportPackageExcel(@Valid ActivityPackageExportReqVO exportReqVO,
                                   HttpServletResponse response) throws IOException {
        List<ActivityPackageDO> list = packageService.getPackageList(exportReqVO);
        // 导出 Excel
        List<ActivityPackageExcelVO> datas = ActivityPackageConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲活动套餐信息.xls", "数据", ActivityPackageExcelVO.class, datas);
    }

}
